package com.yxuan.springboot.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yxuan.springboot.model.AllProfessional;
import com.yxuan.springboot.model.UserLogin;
import com.yxuan.springboot.service.FocusProfessionalService;
import com.yxuan.springboot.service.UserLoginService;
import com.yxuan.springboot.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import com.yxuan.springboot.model.FocusProfessional;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 
 * @author zchao
 *   FocusProfessional控制器
 * @date 2023-06-11 18:57:33
 */
@Controller
@Api(tags = "关注专业表")
@RequestMapping(value = "/focusprofessional", method = {RequestMethod.POST,RequestMethod.GET})
public class FocusProfessionalController extends BaseController<FocusProfessional>{

    @Autowired
    private FocusProfessionalService focusProfessionalService;

    @Autowired
    private UserLoginService userLoginService;

    /**
     *  添加关注院校
     */
    @ApiOperation(value = "添加关注院校", notes = "添加关注院校")
    @RequestMapping("/addFocusProfessional")
    @ResponseBody
    public String addFocusSchool(HttpServletRequest request,Long professionalID) throws Exception {
        String token = request.getHeader("token");
        //解析token
        Claims claims = JwtUtil.parseJWT(token);
        String sub = claims.get("sub", String.class);
        JsonNode subNode = new ObjectMapper().readTree(sub);
        String phoneNumber = subNode.get("phoneNumber").asText();

        UserLogin userLogin = new UserLogin();
        userLogin.setPhoneNumber(phoneNumber);
        UserLogin userLogin1 = userLoginService.findObjectByPramas(userLogin);
        Integer userID = userLogin1.getId();

        FocusProfessional focusProfessional = new FocusProfessional();
        focusProfessional.setProfessionalId(professionalID);
        focusProfessional.setUserId(Long.valueOf(userID));
        focusProfessionalService.save(focusProfessional);
        return BaseController.SUCCESS;
    }

    @ApiOperation(value = "获取关注专业列表", notes = "获取关注专业列表")
    @RequestMapping("/loadTotalProByUserID")
    @ResponseBody
    public List<AllProfessional> loadTotalProByUserID(HttpServletRequest request, Long schoolId) throws Exception {
        String token = request.getHeader("token");
        //解析token
        Claims claims = JwtUtil.parseJWT(token);
        String sub = claims.get("sub", String.class);
        JsonNode subNode = new ObjectMapper().readTree(sub);
        String phoneNumber = subNode.get("phoneNumber").asText();

        UserLogin userLogin = new UserLogin();
        userLogin.setPhoneNumber(phoneNumber);
        UserLogin userLogin1 = userLoginService.findObjectByPramas(userLogin);
        Integer userID = userLogin1.getId();
        return focusProfessionalService.getTotalProByUserID(userID);
    }

    @ApiOperation(value = "取消关注", notes = "取消关注")
    @RequestMapping("/cancelFocusByUserID")
    @ResponseBody
    public String cancelFocusByUserID(HttpServletRequest request,Long professionalID) throws Exception {
        String token = request.getHeader("token");
        //解析token
        Claims claims = JwtUtil.parseJWT(token);
        String sub = claims.get("sub", String.class);
        JsonNode subNode = new ObjectMapper().readTree(sub);
        String phoneNumber = subNode.get("phoneNumber").asText();

        UserLogin userLogin = new UserLogin();
        userLogin.setPhoneNumber(phoneNumber);
        UserLogin userLogin1 = userLoginService.findObjectByPramas(userLogin);
        FocusProfessional focusProfessional = new FocusProfessional();
        Integer userID = userLogin1.getId();

        focusProfessional.setUserId(Long.valueOf(userID));
        focusProfessional.setProfessionalId(professionalID);
        focusProfessionalService.remove(focusProfessional);
        return BaseController.SUCCESS;
    }

    @ApiOperation(value = "是否被关注", notes = "是否被关注")
    @RequestMapping("/isFocused")
    @ResponseBody
    public FocusProfessional isFocused(HttpServletRequest request, Long professionalID) throws Exception {
        String token = request.getHeader("token");
        //解析token
        Claims claims = JwtUtil.parseJWT(token);
        String sub = claims.get("sub", String.class);
        JsonNode subNode = new ObjectMapper().readTree(sub);
        String phoneNumber = subNode.get("phoneNumber").asText();

        UserLogin userLogin = new UserLogin();
        userLogin.setPhoneNumber(phoneNumber);
        UserLogin userLogin1 = userLoginService.findObjectByPramas(userLogin);
        FocusProfessional focusProfessional = new FocusProfessional();
        Integer userID = userLogin1.getId();
        focusProfessional.setUserId(Long.valueOf(userID));
        focusProfessional.setProfessionalId(professionalID);
        return focusProfessionalService.findObjectByPramas(focusProfessional);
    }
}
